package com.zhoujing.cinema.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

/**
 * @author ：ywj
 * @date ：Created in 2019/10/20 22:12
 * @description：
 * @modified By：
 * @version: $
 */
public class SendMsg {
    static Random random=new Random();

    public static void SendMsgs(String phone,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FfPyU3wKNuxusYFuuTb", "UM2nPxrxGgNDU2lhpk8f7V5dm0SCLA");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "zshop");
        request.putQueryParameter("TemplateCode", "SMS_175542737");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    //随机生成包含验证码字符串
    public static String random(int num){
        //初始化种子
        String[] str={"0","1","2","3","4","5","6","7","8","9",
                "a","b","c","d","e","f","g","h","i","j",
                "k","l","m","n","p","q","r","s","t"};
        int number=str.length;
        //接收随机字符
        String text = "";
        //随机产生4个字符的字符串
        for(int i=0;i<num;i++){
            text+=str[random.nextInt(number)];
        }
        return text;
    }
}
